package cn.vendai.com.tools;


import cn.vendai.com.client.SystemClient;
import cn.vendcommon.com.entity.InventoryItem;
import cn.vendcommon.com.entity.Machine;
import cn.vendcommon.com.entity.Result;
import cn.vendcommon.com.entity.vo.UserVO;
import cn.vendcommon.com.utils.UserContext;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工具类
 * Function Calling
 * @author Eleven
 */
@Component
public class InventoryTools {

    @Autowired
    private SystemClient systemClient;

    /**
     * 根据售卖机ID查找商品
     * @param machineId 售卖机ID
     * @return 商品列表
     */
    @Tool("根据售卖机的Id来获取其内部的商品信息")
    public List<InventoryItem> getInventoryItemsByMachineId(@P("售卖机ID") int machineId) {
        if (machineId <= 0) {
            throw new IllegalArgumentException("machineId must be greater than 0");
        }
        List<InventoryItem> data = systemClient.getInventoryByMachineId(machineId).getData();
        // 将每个 item 的 url 字段置为空
        return data.stream()
                .peek(item -> item.setPicture(""))
                .collect(Collectors.toList());
    }

    /**
     * 根据售卖机ID查找售卖机
     * @param id 售卖机ID
     * @return 售卖机
     */
    @Tool(name = "getMachineInfoById", value = "根据售卖机ID来获取当前售卖机的详细信息")
    public Machine getMachineInfoById(@P("售卖机ID") @PathVariable int id){
        if (id < 0) {
            throw new IllegalArgumentException("id must be greater than 0");
        }
        return systemClient.getMachineById(id).getData();
    }

    /**
     * 获取所有售卖机
     * @return 售卖机列表
     */
    @Tool("获取所有的售卖机的详细信息")
    public List<Machine> getAllMachines() {
        return systemClient.getAllMachines().getData();
    }

    /**
     * 获取当前日期时间
     * @return 当前日期时间
     */
    @Tool("获取当前日期时间")
    public LocalDateTime getCurrentDateTime() {
        return LocalDateTime.now();
    }

    /**
     * 获取当前登录用户的个人信息包括余额等
     * @param userId 用户ID
     * @return 用户信息
     */
    @Tool("获取当前登录用户的个人信息包括余额等")
    public UserVO getUserInfo(@P("用户ID") Long userId) {
        if (userId <= 0) {
            throw new IllegalArgumentException("userId must be greater than 0");
        }
        return systemClient.userInfoById(userId).getData();
    }

}
